/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package accesoDatos;

import logicaNegocio.ClaseAsignacion;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import oracle.jdbc.driver.OracleTypes;

/**
 *
 *
 */
public class GestorAsignacion extends Conexion implements Gestor<ClaseAsignacion> {
    private static final String ingresarAsignacion="{call ASIGNAR(?,?,?)}";
    private static final String consultarAsignacion="{?=call CONSULTAR_ASIGNACION(?,?,?)}";
    private static final String eliminarAsignacion="";
    private static final String modificarAsignacion="{call DESASIGNAR(?,?,?)}";
    private static final String listarAsignacion="{?=call LISTAR_ASIGNACION}";
    private static final String consultarAsignacion2="{?=call ESTA_ASIGNADO(?)}";
    
    @Override
    public void ingresarDato(ClaseAsignacion clase) {
        CallableStatement cs = null;
        try{
            if(!Conectar())
                throw null;
            cs = conexion.prepareCall(ingresarAsignacion);
            cs.setString(1, clase.getIdPersona());
            cs.setInt(2, clase.getNumeroMaquina());
            cs.setString(3, clase.getHoraInicio());
            boolean resultado = cs.execute();
            if(resultado){
                throw null;
            }
        }
        catch(SQLException ex){
            System.out.println(ex.getLocalizedMessage());
        }
        finally{
            try{
                if(cs != null)
                    cs.close();
                Desconectar();
            }
            catch(SQLException ex){
                System.out.println(ex.getLocalizedMessage());
            }
            catch(Exception ex){
                System.out.println(ex.getLocalizedMessage());
                return;
            }
        }
    }

    @Override
    public void eliminarDato(String identificacion) {
        CallableStatement cs = null;
        try{
            if(!this.Conectar())
                return;
            cs = this.conexion.prepareCall(eliminarAsignacion);
            cs.setString(1, identificacion);
            int resultado = cs.executeUpdate();
        }
        catch(SQLException ex){
            System.out.println(ex.getLocalizedMessage());
            return;
        }
        finally{
            try{
                if(cs != null)
                    cs.close();
                this.Desconectar();
            }
            catch(SQLException ex){
                System.out.println(ex.getLocalizedMessage());
                return;
            }
            catch(Exception ex){
                System.out.println(ex.getLocalizedMessage());
                return;
            }
        }
    }

    @Override
    public void modificarDato(ClaseAsignacion clase) {
        CallableStatement cs = null;
        try{
            if(!this.Conectar())
                return;
            cs = this.conexion.prepareCall(modificarAsignacion);
            cs.setString(1, clase.getIdPersona());
            cs.setInt(2, clase.getNumeroMaquina());
            cs.setString(3, clase.getHoraInicio());
            int resultado = cs.executeUpdate();
        }
        catch(SQLException ex){
            System.out.println(ex.getLocalizedMessage());
            return;
        }
        finally{
            try{
                if(cs != null)
                    cs.close();
                this.Desconectar();
            }
            catch(SQLException ex){
                System.out.println(ex.getLocalizedMessage());
                return;
            }
            catch(Exception ex){
                System.out.println(ex.getLocalizedMessage());
                return;
            }
        }
    }

    @Override
    public ArrayList<ClaseAsignacion> listarDato() {
        CallableStatement cs = null;
        ArrayList<ClaseAsignacion> coleccion = new ArrayList<ClaseAsignacion>();
        ClaseAsignacion clas = null;
        ResultSet rs = null;
        try{
            if(!this.Conectar())
                return null;
            cs = this.conexion.prepareCall(listarAsignacion);
            cs.registerOutParameter(1, OracleTypes.CURSOR);
            cs.execute();
            rs = (ResultSet)cs.getObject(1);
            while(rs.next()){
                clas = new ClaseAsignacion(
                        rs.getString(1),
                        rs.getInt(2),
                        rs.getString(3),
                        rs.getString(4),
                        rs.getString(5)
                        );
                coleccion.add(clas);
            }
        }
        catch(SQLException ex){
            System.out.println(ex.getLocalizedMessage());
            return null;
        }
        finally{
            try{
                if(rs != null)
                    rs.close();
                if(cs != null)
                    cs.close();
                this.Desconectar();
                if(coleccion == null || coleccion.isEmpty())
                    throw null;
            }
            catch(SQLException ex){
                System.out.println(ex.getLocalizedMessage());
                return null;
            }
            catch(Exception ex){
                System.out.println(ex.getLocalizedMessage());
                return null;
            }
        }
        return coleccion;
    }

    @Override
    public ClaseAsignacion consultarDato(String identificacion) {
        int num = Integer.parseInt(identificacion);
        ResultSet rs = null;
        ClaseAsignacion asignacion = null;
        CallableStatement cs = null;
        try{
            if(!Conectar())
                throw null;
            cs = conexion.prepareCall(consultarAsignacion2);
            cs.registerOutParameter(1, OracleTypes.CURSOR);
            cs.setInt(2, num);
            cs.execute();
            rs = (ResultSet)cs.getObject(1);
            while(rs.next()){
                asignacion = new ClaseAsignacion(
                        rs.getString(1),
                        rs.getInt(2),
                        rs.getString(3),
                        rs.getString(4),
                        rs.getString(5)
                        );
            }
        }
        catch(SQLException ex){
            System.out.println(ex.getMessage());
            return null;
        }
        finally{
            try{
                if(rs != null)
                    rs.close();
                if(cs != null)
                    cs.close();
                Desconectar();
                if(asignacion == null)
                    throw null;
            }
            catch(SQLException ex){
                System.out.println(ex.getMessage());
                return null;
            }
            catch(Exception ex){
                System.out.println(ex.getMessage());
                return null;
            }
        }
        return asignacion;
    }

    public ClaseAsignacion consultarAsignacion(String identificacion, int numeroMaquina, String hora) {
        ResultSet rs = null;
        ClaseAsignacion asignacion = null;
        CallableStatement cs = null;
        try{
            if(!Conectar())
                throw null;
            cs = conexion.prepareCall(consultarAsignacion);
            cs.registerOutParameter(1, OracleTypes.CURSOR);
            cs.setString(2, identificacion);
            cs.setInt(3, numeroMaquina);
            cs.setString(4, hora);
            cs.execute();
            rs = (ResultSet)cs.getObject(1);
            while(rs.next()){
                asignacion = new ClaseAsignacion(
                        rs.getString(1),
                        rs.getInt(2),
                        rs.getString(3),
                        rs.getString(4),
                        rs.getString(5)
                        );
            }
        }
        catch(SQLException ex){
            System.out.println(ex.getMessage());
            return null;
        }
        finally{
            try{
                if(rs != null)
                    rs.close();
                if(cs != null)
                    cs.close();
                Desconectar();
                if(asignacion == null)
                    throw null;
            }
            catch(SQLException ex){
                System.out.println(ex.getMessage());
                return null;
            }
            catch(Exception ex){
                System.out.println(ex.getMessage());
                return null;
            }
        }
        return asignacion;
    }

}
